package com.cyp.school.controller;

import com.cyp.school.dto.Result;
import com.cyp.school.entity.Comment;
import com.cyp.school.entity.User;
import com.cyp.school.service.CommentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/api/comment")
public class CommentController {
    
    @Resource
    private CommentService commentService;
    
    @PostMapping
    public Result<Comment> addComment(@RequestBody Comment comment, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return Result.error(401, "未登录");
        }
        
        comment.setUserId(user.getId());
        return commentService.addComment(comment);
    }
    
    @GetMapping("/blog/{blogId}")
    public Result<List<Comment>> getCommentsByBlogId(@PathVariable Integer blogId) {
        return commentService.getCommentsByBlogId(blogId);
    }
    
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteComment(@PathVariable Integer id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return Result.error(401, "未登录");
        }
        
        return commentService.deleteComment(id);
    }
    
    @GetMapping("/count/{blogId}")
    public Result<Integer> getCommentCount(@PathVariable Integer blogId) {
        return commentService.getCommentCount(blogId);
    }
} 